<template>
  <div>
    <el-row>
      <el-col :span="24">
        <div class="search-box">
          <el-form :inline="true" size="medium">
            <el-form-item>
              <el-button type="primary" @click="showAddDialogForm">新增</el-button>
            </el-form-item>
          </el-form>

          <el-form :inline="true" size="medium" :model="searchForm">
            <el-form-item>
              <el-button type="primary" @click="search">刷新</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
    </el-row>

    <el-table
      :data="tableData"
      :header-cell-style="{backgroundColor:'#f4f4f5',textAlign:'center'}"
      :cell-style="{textAlign:'center'}"
      :height="tableHeight"
      border
      style="width: 100%">
      <el-table-column
        prop="serialNumber"
        label="编号"
        min-width="120">
      </el-table-column>
      <el-table-column
        prop="name"
        label="级别名称"
        min-width="120">
      </el-table-column>
      <el-table-column
        prop="quota"
        label="日消费限额"
        min-width="120">
      </el-table-column>
      <el-table-column
        prop="subsidy"
        label="日补贴(次)"
        min-width="120">
      </el-table-column>
      <el-table-column
        prop="workSubsidy"
        label="考勤日补贴(次)"
        min-width="120">
      </el-table-column>
      <el-table-column
        prop="subsidyQuota"
        label="月补贴限额(元)"
        min-width="120">
      </el-table-column>
      <el-table-column
        prop="equipment"
        label="限定设备"
        min-width="120">
      </el-table-column>
      <el-table-column
        prop="orderFood"
        label="是否与订餐联动"
        min-width="120">
        <template scope="scope">
          <span v-if="scope.row.orderFood==1">是</span>
          <span v-else-if="scope.row.orderFood==2">否</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="overdraft"
        label="是否允许透支"
        min-width="120">
        <template scope="scope">
          <span v-if="scope.row.overdraft==1">是</span>
          <span v-else-if="scope.row.overdraft==2">否</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="dayOverdraft"
        label="日透支额度(元)"
        min-width="120">
      </el-table-column>
      <el-table-column
        prop="totalityOverdraft"
        label="总透支额度(元)"
        min-width="120">
      </el-table-column>


      <el-table-column
        prop="address"
        label="操作"
        min-width="250">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
       <span class="demonstration"></span>
       <el-pagination
         @size-change="handleSizeChange"
         @current-change="handleCurrentChange"
         :current-page="currentPage"
         :page-sizes="[10, 20, 50, 100,200]"
         :page-size="pageSize"
         layout="total, sizes, prev, pager, next, jumper"
         :total="total">
       </el-pagination>
     </div>

    <el-dialog :title="editForm.id==0?'新增':'修改'" :visible.sync="editFormVisible" width="500px" :close-on-click-modal="false">
      <el-form :model="editForm" ref="editForm" :rules="editRules">
        <el-form-item label="编号" label-width="120px" prop="serialNumber">
          <el-input v-model="editForm.serialNumber" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="名称" label-width="120px" prop="name">
          <el-input v-model="editForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="日消费限额(元)" label-width="120px" prop="quota">
          <el-input type="number" v-model="editForm.quota" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="日补贴(次)" label-width="120px" prop="subsidy">
          <el-input type="number" v-model="editForm.subsidy" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="考勤日补贴(次)" label-width="120px" prop="workSubsidy">
          <el-input type="number" v-model="editForm.workSubsidy" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="月补贴限额(元)" label-width="120px" prop="subsidyQuota">
          <el-input type="number" v-model="editForm.subsidyQuota" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="限定设备" label-width="120px" prop="equipment">
          <el-input v-model="editForm.equipment" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="是否与订餐联动" label-width="120px" prop="orderFood">
          <el-select v-model="editForm.orderFood" placeholder="请选择">
            <el-option :key="1" label="是" :value="1"></el-option>
            <el-option :key="2" label="否" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否允许透支" label-width="120px" prop="overdraft">
          <el-select v-model="editForm.overdraft" placeholder="请选择">
            <el-option :key="1" label="是" :value="1"></el-option>
            <el-option :key="2" label="否" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="editForm.overdraft == 1 "label="日透支额度(元)" label-width="120px" prop="dayOverdraft">
          <el-input type="number" v-model="editForm.dayOverdraft" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item v-if="editForm.overdraft == 1 "label="总透支额度(元)" label-width="120px" prop="totalityOverdraft">
          <el-input type="number" v-model="editForm.totalityOverdraft" autocomplete="off"></el-input>
        </el-form-item>




      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editFormVisible = false">取 消</el-button>
        <el-button type="primary" @click.native.prevent="submitForm" :loading="loading">{{ editForm.id==0?'添 加':'修 改' }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {validPhone} from '@/common/utils/validate.js'
import request from '@/common/utils/request.js'
import util from '@/common/utils/util.js'
import {getGoRankList, addGoRank, updateGoRank, deleteGoRank} from "@/api/go_rank";

export default {
  name: "goRank",
  data() {
    return {
      total: 0,
      pageSize: 10,
      currentPage: 1,
      tableData: [],
      tableHeight: 200,
      editFormVisible:false,
      loading: false,
      editForm: {
        id: 0,
        name: '',
        serialNumber: '',
        quota: '',
        subsidy: '',
        workSubsidy: '',
        subsidyQuota: '',
        equipment: '',
        orderFood: '',
        overdraft: '',
        dayOverdraft: '',
        totalityOverdraft: '',

      },
      editRules: {
        serialNumber: [{ required: true, message:"请输入编号", trigger: 'blur'}],
        name: [{ required: true, message:"请输入名称", trigger: 'blur'}],
        // quota: [{ required: true, message:"请输入名称", trigger: 'blur'}],
        // totalityOverdraft: [{ required: true, message:"请输入名称", trigger: 'blur'}],
        // subsidy: [{ required: true, message:"请输入名称", trigger: 'blur'}],
        // workSubsidy: [{ required: true, message:"请输入名称", trigger: 'blur'}],
        // subsidyQuota: [{ required: true, message:"请输入名称", trigger: 'blur'}],
        // equipment: [{ required: true, message:"请输入名称", trigger: 'blur'}],
        // orderFood: [{ required: true, message:"请输入名称", trigger: 'blur'}],
        // overdraft: [{ required: true, message:"请输入名称", trigger: 'blur'}],
        // dayOverdraft: [{ required: true, message:"请输入名称", trigger: 'blur'}],
      },
      loadingDiv: null,
      searchForm: {

      },
      roleList:[],
    }
  },
  watch:{

  },
  mounted() {
    this.getDataList(this.currentPage);

    this.$nextTick(() => {
        this.tableHeight = document.documentElement.clientHeight - 198;
    })
    window.onresize = () => {
      this.$nextTick(() => {
          this.tableHeight = document.documentElement.clientHeight - 198;
      })
    }
  },
  methods: {

    showAddDialogForm() {
      if(this.$refs.editForm){
        this.$refs.editForm.clearValidate();
      }
      this.editForm.id = 0
      this.editForm.name = "";
      this.editForm.serialNumber = "";
      this.editForm.quota = "";
      this.editForm.subsidy = "";
      this.editForm.workSubsidy = "";
      this.editForm.subsidyQuota = "";
      this.editForm.equipment = "";
      this.editForm.orderFood = "";
      this.editForm.overdraft = "";
      this.editForm.dayOverdraft = "";
      this.editForm.totalityOverdraft = "";
      this.editFormVisible = true;
    },
    handleEdit(index, row) {

      if(this.$refs.editForm){
        this.$refs.editForm.clearValidate();
      }
      this.editForm.id = row.id;
      this.editForm.name = row.name;
      this.editForm.serialNumber = row.serialNumber;
      this.editForm.quota = row.quota;
      this.editForm.subsidy = row.subsidy;
      this.editForm.workSubsidy = row.workSubsidy;
      this.editForm.subsidyQuota = row.subsidyQuota;
      this.editForm.equipment = row.equipment;
      this.editForm.orderFood = row.orderFood;
      this.editForm.overdraft = row.overdraft;
      this.editForm.dayOverdraft = row.dayOverdraft;
      this.editForm.totalityOverdraft = row.totalityOverdraft;
      this.editFormVisible = true;

    },
    submitForm() {
      this.$refs.editForm.validate(valid => {
        if(valid) {
          this.loading = true;
          let loadingText = "添加中...";
          if(this.editForm.id){
            loadingText = "修改中...";
          }
          const loadingDiv = util.showLoading(loadingText)
          let func = updateGoRank
          if(this.editForm.id == 0){
            func = addGoRank
          }
          func(this.editForm).then(res => {
            let title = "修改"
            if(this.editForm.id == 0){
              title = "添加"
            }
            if(res.code == 0) {
              this.$alert(res.msg, title+"失败", {dangerouslyUseHTMLString:true,type:'warning'})
            }else if(res.code == 1) {
              this.$alert(res.msg, title+"成功", {dangerouslyUseHTMLString:true,type:'success'})
                .then(()=> {
                  this.editFormVisible=false;
                  this.getDataList(this.currentPage)
                })
            }
            this.loading = false;
            loadingDiv.close();
          }).catch(res => {
            this.loading = false;
            loadingDiv.close();
          })
        }
      });
    },
    handleDelete(index, row) {
      let loadingDiv;
      this.$confirm("是否确认删除："+row.name+"?","删除确认").then(res=>{
        if(res='confirm'){
          loadingDiv = util.showLoading("删除中...")
          return deleteGoRank({id: row.id})
        }
      }).then(res => {
        loadingDiv.close();
        if(res.code == 0) {
          this.$alert(res.msg, "删除失败", {dangerouslyUseHTMLString:true,type:'warning'})
        }else if(res.code == 1) {
          this.$alert(res.msg, "删除成功", {dangerouslyUseHTMLString:true,type:'success'})
            .then(res=>{
              this.editFormVisible=false;
              this.getDataList(this.currentPage)
            })
        }
      }).catch(res=>{
        if (loadingDiv){
          loadingDiv.close();
        }
      })
    },
    getDataList(pageNum){
      const loadingDiv = util.showLoading()

      getGoRankList({
        pageSize: this.pageSize,
        pageNum: pageNum,
      }).then(res => {
        if(res.code == 0) {
          this.$message({
            showClose: true,
            message: res.msg,
            type: 'error'
          });
        }else if(res.code == 1) {
          let data = res.data;
          this.currentPage = pageNum;
          this.total = data.total;
          this.tableData = data.dataList;
        }
        loadingDiv.close();
      }).catch(res => {
        loadingDiv.close();
      })
    },
    search() {
      this.getDataList(this.currentPage)
    },
    handleSizeChange(pageSize) {
            this.pageSize = pageSize;
            this.getDataList(1)
          },
    handleCurrentChange(pageNum) {
            this.getDataList(pageNum);
          },
  }
}
</script>

<style scoped lang="scss">
  // @import "~@/common/styles/element-variables.scss";
  .search-box {
    background-color: #FFFFFF;
    min-height: 36px;
    line-height: 36px;
    padding-top: 2px;
    padding-bottom: 6px;
    .el-form-item {
      margin-bottom: 0px !important;
    }
    .el-form--inline {
      display: inline-block;
    }
  }
  .pagination-box {
    text-align: left;
    margin-top: 10px;
  }
</style>
<style lang="scss">
  .el-dialog__header {

  }
  .el-dialog__body {
    padding-top: 10px;
    padding-bottom: 0px;
  }
  .el-dialog__footer {
    padding-top: 0px;
  }
</style>
